package com.data.modules.online.cgreport.util;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.SubJoin;
import net.sf.jsqlparser.statement.select.SubSelect;
import net.sf.jsqlparser.statement.select.ValuesList;
import net.sf.jsqlparser.statement.select.WithItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CgReportSqlServerParse {

    private static final Log f386a = LogFactory.getLog(CgReportSqlServerParse.class);

    private static Pattern f387b = Pattern.compile(":[ tnx0Bfr]*[0-9a-z.A-Z_]+");

    private static String f388c = ".";

    private static String f389d = "@@@";

    public String getSelectStr(String str) throws JSQLParserException {
        List<String> list;
        Statement select;
        List<String> list2 = null;
        try {
            select = CCJSqlParserUtil.parse(str);
        } catch (JSQLParserException e) {
            if (e.toString().indexOf(f388c) != -1) {
                list = getGroupList(str);
                for (String str2 : list) {
                    str = str.replace(str2, str2.replace(f388c, f389d));
                }
                select = CCJSqlParserUtil.parse(str);
            } else {
                e.printStackTrace();
                list = null;
                select = null;
            }
            f386a.debug(" --- JSQLParser with DIAN --- convert begin sql=" + str);
            list2 = list;
        }
        Select select2 = (Select) select;
        m343a(select2.getSelectBody());
        String select3 = select2.toString();
        if (list2 == null) {
            return select3;
        }
        for (String str3 : list2) {
            select3 = select3.replace(str3.replace(f388c, f389d), str3.replace(f389d, f388c));
        }
        f386a.debug(" --- JSQLParser with DIAN --- convert end sql=" + str);
        return select3;
    }

    private static List<String> getGroupList(String str) {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        Matcher matcher = f387b.matcher(str);
        while (matcher.find()) {
            if (!(matcher.group() == null || matcher.group().indexOf(f388c) == -1)) {
                linkedHashSet.add(matcher.group());
            }
        }
        return new ArrayList(linkedHashSet);
    }

    private void m343a(SelectBody selectBody) {
        if (selectBody instanceof PlainSelect) {
            m342a((PlainSelect) selectBody);
        } else if (selectBody instanceof WithItem) {
            WithItem withItem = (WithItem) selectBody;
            if (withItem.getSelectBody() != null) {
                m343a(withItem.getSelectBody());
            }
        } else {
            SetOperationList setOperationList = (SetOperationList) selectBody;
            if (setOperationList.getSelects() != null && setOperationList.getSelects().size() > 0) {
                for (SelectBody withItem2 : setOperationList.getSelects()) {
                    if (withItem2 instanceof PlainSelect) {
                        m342a((PlainSelect) withItem2);
                    } else if (withItem2 instanceof WithItem) {
                        WithItem withItem3 = (WithItem) withItem2;
                        if (withItem3.getSelectBody() != null) {
                            m343a(withItem3.getSelectBody());
                        }
                    }
                }
            }
            if (!getOrderBy(setOperationList.getOrderByElements())) {
                setOperationList.setOrderByElements((List) null);
            }
        }
    }

    private void m342a(PlainSelect plainSelect) {
        if (!getOrderBy(plainSelect.getOrderByElements())) {
            plainSelect.setOrderByElements((List) null);
        }
        if (plainSelect.getFromItem() != null) {
            m341a(plainSelect.getFromItem());
        }
        if (plainSelect.getJoins() != null && plainSelect.getJoins().size() > 0) {
            for (Join join : plainSelect.getJoins()) {
                if (join.getRightItem() != null) {
                    m341a(join.getRightItem());
                }
            }
        }
    }

    private void m341a(FromItem fromItem) {
        if (fromItem instanceof SubJoin) {
            SubJoin subJoin = (SubJoin) fromItem;
            if (subJoin.getJoinList() != null) {
                for (Join join : subJoin.getJoinList()) {
                    if (join.getRightItem() != null) {
                        m341a(join.getRightItem());
                    }
                }
            }
            if (subJoin.getLeft() != null) {
                m341a(subJoin.getLeft());
            }
        } else if (fromItem instanceof SubSelect) {
            SubSelect subSelect = (SubSelect) fromItem;
            if (subSelect.getSelectBody() != null) {
                m343a(subSelect.getSelectBody());
            }
        } else if (!(fromItem instanceof ValuesList) && (fromItem instanceof LateralSubSelect)) {
            LateralSubSelect lateralSubSelect = (LateralSubSelect) fromItem;
            if (lateralSubSelect.getSubSelect() != null) {
                SubSelect subSelect2 = lateralSubSelect.getSubSelect();
                if (subSelect2.getSelectBody() != null) {
                    m343a(subSelect2.getSelectBody());
                }
            }
        }
    }

    private boolean getOrderBy(List<OrderByElement> list) {
        if (list == null) {
            return false;
        }
        for (OrderByElement orderByElement : list) {
            if (orderByElement.toString().toUpperCase().contains("?")) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        try {
            System.out.println("select * from sys_user order by CASE WHEN sex='1' THEN create_time else update_time END");
            System.out.println(((CgReportSqlServerParse) CgReportSqlServerParse.class.newInstance()).getSelectStr("select * from sys_user order by CASE WHEN sex='1' THEN create_time else update_time END"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
